﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KYO.DAL;
using BusinessLayerHelper;
using ObjectPersistence;

public partial class Admin_Controls_Mod_Information_Information : System.Web.UI.UserControl,IView<Information>
{
    InformationAssembler inforService = new InformationAssembler();
    CategoryAssembler categoryService = new CategoryAssembler();
    PersistentList<Information> infors = new PersistentList<Information>();
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected override void OnInit(EventArgs e)
    {

        LoadCategoryData();

        base.OnInit(e);
    }

    #region IView<Information> Members
    private Information model;
    public Information Model
    {
        set { model = value; }
        get { return model; }

    }

    public void UpdateView(Information o)
    {
        //LoadCategoryData();
        Model = o;
        tb_Title.Text = Model.Title;
        tb_Odering.Text =""+ Model.Odering;
        cb_Published.Checked = Model.Published;
        tb_ImageUrl.Text = Model.ImageUrl;
        editorAbstract.Value = Model.Abstract;
        editorContent.Value = Model.Content;
        Categories = o.CategoryColl;
    }

    public Information UpdateModel(Information o)
    {
        Model = o;
        Model.Title = tb_Title.Text;
        Model.Published = cb_Published.Checked;
        Model.Odering = int.Parse(tb_Odering.Text);
        Model.Abstract = editorAbstract.Value.Trim();
        Model.Content = editorContent.Value.Trim();
        Model.ImageUrl = tb_ImageUrl.Text;
        Model.CategoryColl.Clear();
        //Here some problem here, I will update later
        inforService.MakePersistent(Model,true);
        Model.CategoryColl = Categories;
        
        return Model;
    }

    #endregion

    void LoadCategoryData()
    {
        PersistentList<Category> categories = categoryService.ReadCollectionAll();

        lb_Categories.DataSource = categories;
        lb_Categories.DataTextField = "Title";
        lb_Categories.DataValueField = "CategoryId";
        lb_Categories.DataBind();
        

    }

    public PersistentList<Category> Categories
    {

        set
        {
            PersistentList<Category> categories = value;
            List<int> list = new List<int>();
            foreach (Category c in categories)
            {
                list.Add(c.CategoryID);

            }

            for (int i = 0; i < lb_Categories.Items.Count; i++)
            {
                if (list.Contains(int.Parse(lb_Categories.Items[i].Value)))
                {
                    lb_Categories.Items[i].Selected = true;
                }

            }

        }

        get
        {
            PersistentList<Category> categories = new PersistentList<Category>();
            for (int i = 0; i < lb_Categories.Items.Count; i++)
            {
                if (lb_Categories.Items[i].Selected)
                {
                    //Get Tabid
                    Category category = categoryService.ReadSingleByKey(int.Parse(lb_Categories.Items[i].Value));
                    //tab.IsNew = false;
                    category.IsNew = false;
                    categories.Add(category);
                }

            }
            return categories;
        }

    }
}
